package com.cooper.dao.admin;

import java.util.List;

import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.ReturnGeneratedKeys;
import net.paoding.rose.jade.annotation.SQL;

import com.cooper.entity.MallCategoryEntity;
import com.cooper.vo.MallCategoryVo;


/**
 * 官方商城的类目
 * @Description: <br>
 * @Date: Oct 7, 2014 3:40:56 PM <br>
 * @Copyright (c) 2014 MiXuan <br> * 
 * @since 1.0
 * @author coral
 */
@DAO
public interface MallCategoryDAO {
	
	@ReturnGeneratedKeys
	@SQL("insert into d_mall_category(creator,modifier,name,status,parent_id,sort_no,has_child,cata_level,full_id_path,full_name_path) values("
			+ ":1.creator,:1.modifier,:1.name,:1.status,:1.parentId,:1.sortNo,:1.hasChild,:1.cataLevel,:1.fullIdPath,:1.fullNamePath)")
	Long addMallCategory(MallCategoryEntity category)throws Exception;
	
	@SQL("update d_mall_category set deleted=:1.deleted, modifier=:1.modifier,gmt_modified=:1.gmt_modified,status=:1.status," +
			"name=:1.name,parent_id=:1.parentId,sort_no=:1.sortNo,has_child=:1.hasChild,cata_level=:1.cataLevel,full_id_path=:1.fullIdPath,full_name_path=:1.fullNamePath"
			+ " where id=:1.id"
	)
	void updateMallCategory(MallCategoryEntity category)throws Exception;
	
	@SQL("update d_mall_category set full_name_path= concat(:1.fullNamePath, substring(full_path, char_length(CONCAT(:2,'/'))))"
			+ " where full_id_path like CONCAT(:1.fullIdPath,'/%') ")
	void updateChildFullPathWhenChangeName(MallCategoryEntity entity, String oldFullPath) throws Exception;
	
	@SQL("select * from  d_mall_category where id=:1")
	MallCategoryEntity getMallCategory(Long categoryId);
	

	@SQL("select * from  d_mall_category d where deleted='n' "
			+ " #if(null!=:1.id){ and id=:1.id } "
			+ " #if(null!=:1.parentId){ and parent_id=:1.parentId } "
			+ " #if(null !=:1.name && '' != :1.name){ and name like CONCAT('%',:1.name,'%')} "
			+ " #if(null !=:1.fullNamePath && '' != :1.fullNamePath){ and full_name_path like CONCAT('%',:1.fullNamePath,'%')} "
			+ " #if(null!=:1.cata_level){ and cata_level=:1.cataLevel } "
			+ " #if(null!=:1.hasChild){ and has_child=:1.hasChild } " +
			"order by sort_no"
	)
	List<MallCategoryEntity> queryMallCategory(MallCategoryVo vo)throws Exception;
	
	
	@SQL("select d.*,(SELECT  CASE WHEN COUNT(1)=0 THEN 0 ELSE 1 END FROM d_mall_cat_search where deleted='n' and mall_cat_id=d.id) as checked from  d_mall_category d where deleted='n' "
			+ " #if(null!=:1.id){ and id=:1.id } "
			+ " #if(null!=:1.parentId){ and parent_id=:1.parentId } "
			+ " #if(null !=:1.name && '' != :1.name){ and name like CONCAT('%',:1.name,'%')} "
			+ " #if(null !=:1.fullNamePath && '' != :1.fullNamePath){ and full_name_path like CONCAT('%',:1.fullNamePath,'%')} "
			+ " #if(null!=:1.cata_level){ and cata_level=:1.cataLevel } "
			+ " #if(null!=:1.hasChild){ and has_child=:1.hasChild } " +
			"order by sort_no"
	)
	List<MallCategoryEntity> queryMallCategoryChecked(MallCategoryVo vo);
	
	@SQL("select max(sort_no) from  d_mall_category where parent_id=:1 and deleted='n'")
	Long findMaxSortNo(Long parentId);
	
	@SQL("select name from d_mall_category t where t.deleted='n' and id=:1 ")
	String findName(long categoryId) throws Exception;
	
}
